import { NSpin, type SpinProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type SpinFactoryConstructorType = {
  props?: SpinProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  descriptionSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
};

export class SpinFactory {
  private attrs: HTMLAttributes = {};
  private props: SpinProps = {};
  private defaultSlot: BaseComponentType = null;
  private descriptionSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;

  constructor(data?: SpinFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.descriptionSlot) this.setDescription(data.descriptionSlot);
    if (data?.iconSlot) this.setIcon(data.iconSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: SpinProps) {
    this.props = props;
    return this;
  }

  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }

  setDescription(descriptionSlot: BaseComponentType) {
    this.descriptionSlot = descriptionSlot;
    return this;
  }

  setIcon(iconSlot: BaseComponentType) {
    this.iconSlot = iconSlot;
    return this;
  }

  create() {
    return (
      <NSpin {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
          description:
            this.descriptionSlot != null
              ? () => getSlotsDom(this.descriptionSlot)
              : null,
          icon: this.iconSlot != null ? () => getSlotsDom(this.iconSlot) : null,
        }}
      </NSpin>
    );
  }
}
